<?php
// +----------------------------------------------------------------------
// | jpush [ NO ZUO NO DIE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2010-2015 http://www.jula.cc All rights reserved.
// +----------------------------------------------------------------------
// | Author: Hanj
// +----------------------------------------------------------------------
 
//----------------------------------
// Jpush-消息推送
//----------------------------------
class jpush {
 
    private $pushUrl = 'https://api.jpush.cn/v3/push';
 
    public function __construct($config){
        $this->appKey       = $config['appKey'];
        $this->masterSecret = $config['appSecret'];
    }
 
    /**
     * 根据手机号码及面额查询是否支持充值
     * @param  array  $aid     [用户aid数组,默认=all]
     * @param  string $title   [发送消息标题]
     * @param  string $msg     [发送消息]
     * @param  array  $extras  [推送结构]
     * @param  array  $platform[推送平台array('ios','android'),默认=all]
     * @return  boolean
     */
    public function push($aid='all', $title='', $msg='', $extras=array(), $platform='all'){


        $params['platform'] = $platform;
        $params['audience'] = $aid;
        if (!is_array($aid) AND $aid != 'all') {
            
            return FALSE;   
        } elseif(is_array($aid)) {
            $params['audience']['alias'] = $aid;
        }
        $params['notification']['alert'] = '您有条新消息';

        // ANDRIOD
        $params['notification']['android'] = array("alert"     => $msg
                                                 , "title"     => $title
                                                 , "builder_id"=>1
                                                 , "extras"    => $extras);

        // IOS
        $params['notification']['ios'] = array("alert"  => $msg
                                             , "sound"  => "default"
                                             , "badge"  => "+1"
                                             , "extras" => $extras);

        // RETURN
        return $this->Curl($this->pushUrl, $params);

    }
 
    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    public function Curl($url,$params=false){
        if(function_exists('curl_init')) {
            
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-type:application/json;charset=UTF-8',
            'Content-Length: ' . strlen(json_encode($params))));
            //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"); // 模拟用户使用的浏览器

            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, $this->appKey.':'.$this->masterSecret);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);// 30s to timeout.
            $response = curl_exec($ch);
            if(curl_errno($ch)){
                //error
                return array(FALSE, 'Curl error: '.curl_error($ch));
            }
            $reslut = curl_getinfo($ch);
            $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
            curl_close($ch);
            return array($httpCode=='200', $response);

        } else {
            throw new Exception('Do not support CURL function.');
        }
    }
}